package com.peoit.honeycomb.controller;

import com.peoit.honeycomb.application.BannerApplication;
import com.peoit.honeycomb.application.utils.ZqCustomException;
import com.peoit.honeycomb.domain.Banner;
import com.peoit.honeycomb.dto.BannerDto;
import com.peoit.honeycomb.dto.BannerPageDto;
import com.peoit.honeycomb.dto.CommonResultJsonDto;
import com.peoit.honeycomb.dto.PageCommonResultDto;
import com.peoit.honeycomb.utils.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.List;
import java.util.logging.Logger;

/**
 * 图片轮播
 * Created by zhouqiu on 2015/9/24.
 */
@Controller
@RequestMapping("/admin/banner")
public class BannerController extends HoneycombBaseController {
	Logger logger = Logger.getLogger(ProjectController.class.getName());

	@Autowired
	BannerApplication bannerApplication;

	@RequestMapping("/list/{offset}")
	public ModelAndView list(BannerPageDto dto,@PathVariable int offset) {
		dto.setOffset(offset);
		List<BannerDto> dtos = bannerApplication.findByWherePage(dto);

		Long count = bannerApplication.findPersonCount(dto);

		PageCommonResultDto pageCommonResultDto = new PageCommonResultDto();
		pageCommonResultDto.setResult(dtos);
		pageCommonResultDto.setPageSize(dto.getPageSize());
		pageCommonResultDto.setCount(count);
		pageCommonResultDto.setOffset(offset);


		return view("/jsp/banner/bannerlist", pageCommonResultDto);
	}

	/**
	 * 跳转到banner新增
	 * 
	 * @return
	 */
	@RequestMapping("/toAdd")
	public ModelAndView toAdd() {
		return view("/jsp/banner/banneradd");
	}

	@RequestMapping("/add")
	public @ResponseBody CommonResultJsonDto add(HttpServletRequest request ,BannerDto dto,@RequestParam(required = false, value = "bannerpic") MultipartFile bannerpic) {

		CommonResultJsonDto result = CommonResultJsonDto.success("/admin/banner/list/0","成功哦！！");
		try {

			if (bannerpic.getOriginalFilename() != null && !bannerpic.getOriginalFilename().equals("")) {
				String originalFilename = bannerpic.getOriginalFilename();
				UploadUtil uploadUtil = new UploadUtil();
				String path = uploadUtil.getUploadPicPath(bannerpic);
				dto.setName(originalFilename);
				dto.setPath(path);
			}
			boolean b = bannerApplication.addBanner(dto);
		}catch (ZqCustomException c){
			result = CommonResultJsonDto.fail(c.getMessage());
		} catch (Exception ex) {
			ex.printStackTrace();
			result = CommonResultJsonDto.fail("系统错误！！");
		}

		return result;
	}

	/**
	 * 跳转到banner详情
	 *
	 * @return
	 */
	@RequestMapping("/toxq/{id}")
	public ModelAndView toxq(@PathVariable String id) {
		Banner banner = (Banner)this.bannerApplication.find(Banner.class, id);
		BannerDto bannerDto = BannerDto.toDto(banner);
		return view("/jsp/banner/banneredit",bannerDto);
	}

	@RequestMapping("/deleteBanner")
	public @ResponseBody CommonResultJsonDto deleteRole( String id) {
		CommonResultJsonDto resultJsonDto = CommonResultJsonDto.success("删除成功！");
		try{
			this.bannerApplication.deleteBy(Banner.class, id);
		}catch (Exception c){
			resultJsonDto = CommonResultJsonDto.success("系统错误！");
		}
		return resultJsonDto;
	}


}
